package com.tengits.mqttmonitor.rnlibs.utils;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.tengits.mqttmonitor.rnlibs.mqttbroker.NetStateChangeObserver;
import com.tengits.mqttmonitor.rnlibs.mqttbroker.NetworkChangeReceiver;
import com.tengits.mqttmonitor.util.Utils;

public class UtilLibraryModule extends ReactContextBaseJavaModule implements NetStateChangeObserver, LifecycleEventListener {
    private ReactContext mReactContext = null;

    public UtilLibraryModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mReactContext = reactContext;
        mReactContext.addLifecycleEventListener(this);
        NetworkChangeReceiver.registerObserver(this);
    }

    private static final String TAG = UtilLibraryModule.class.getSimpleName();
    @NonNull
    @Override
    public String getName() {
        return "UtilsLibrary";
    }

    @ReactMethod
    public void addListener(String eventName) {
        // Set up any upstream listeners or background tasks as necessary
    }
    @ReactMethod
    public void removeListeners(Integer count) {
        // Remove upstream listeners, stop unnecessary background tasks
    }

    private void sendEvent(ReactContext reactContext,
                           String eventName,
                           @Nullable WritableMap params) {
        if(!reactContext.hasActiveReactInstance()) {
            return;
        }
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);
    }

    @ReactMethod
    public void gotoActivity(final String acName) {
        Intent intent = new Intent();
        String packageName = getReactApplicationContext().getPackageName();
        Log.e(TAG, packageName);
        intent.setClassName(packageName, packageName+"."+acName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getReactApplicationContext().startActivity(intent);
    }

    @ReactMethod
    public void getMachineIp(Promise promise) {
        String address = Utils.getBrokerURL(getReactApplicationContext());
        if(promise != null) {
            promise.resolve(address);
        }
    }

    @Override
    public void onDisconnect() {
        WritableMap map = Arguments.createMap();
        map.putString("event", "disconnect");
        sendEvent(mReactContext, "net_events", map);
    }

    @Override
    public void onMobileConnect() {
        WritableMap map = Arguments.createMap();
        map.putString("event", "connect");
        map.putString("type", "mobile");
        sendEvent(mReactContext, "net_events", map);
    }

    @Override
    public void onWifiConnect() {
        WritableMap map = Arguments.createMap();
        map.putString("event", "connect");
        map.putString("type", "wifi");
        sendEvent(mReactContext, "net_events", map);
    }

    @Override
    public void onHostResume() {

    }

    @Override
    public void onHostPause() {

    }

    @Override
    public void onHostDestroy() {
        try{
            NetworkChangeReceiver.unRegisterObserver(this);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
